-- SPDX-License-Identifier: GPL-3.0-or-later

local extension = Package:new("old_change1")
-- extension.extensionName = "vupslash"
extension.extensionName = "VslTest"
-- extension.game_modes_whitelist = { 'nos_heg_mode', 'new_heg_mode' }
-- dofile "packages/vupslash/game_rule.lua"

-- local VslUtil = require "packages/vupslash/util"
local VslUtil = require "packages/VslTest/util"

--------------------------------------------------
--测试技能：超级英姿
--------------------------------------------------

local super_yingzi = fk.CreateTriggerSkill{
	name = "superyingzi",
	anim_type = "drawcard",
	events = {fk.DrawNCards},
	on_use = function(self, event, target, player, data)
		data.n = data.n + 20
	end,
}

--------------------------------------------------
--测试技能：摸指定牌
--------------------------------------------------

local v_cheat = fk.CreateActiveSkill{
	name = "v_cheat",
	anim_type = "drawcard",
	can_use = function(self, player)
		return true
	end,
	on_use = function(self, room, effect)
		local from = room:getPlayerById(effect.from)
		local cardTypeName = room:askForChoice(from, { 'basic', 'trick', 'equip' }, "cheat")
		local cardType = Card.TypeBasic
		if cardTypeName == 'trick' then
			cardType = Card.TypeTrick
		elseif cardTypeName == 'equip' then
			cardType = Card.TypeEquip
		end

		local allCardIds = Fk:getAllCardIds()
		local allCardMapper = {}
		local allCardNames = {}
		for _, id in ipairs(allCardIds) do
			local card = Fk:getCardById(id)
			if card.type == cardType then
				if allCardMapper[card.name] == nil then
					table.insert(allCardNames, card.name)
				end

				allCardMapper[card.name] = allCardMapper[card.name] or {}
				table.insert(allCardMapper[card.name], id)
			end
		end

		if #allCardNames == 0 then
			return
		end

		local cardName = room:askForChoice(from, allCardNames, "cheat")
		local toGain = nil
		if #allCardMapper[cardName] > 0 then
			toGain = allCardMapper[cardName][math.random(1, #allCardMapper[cardName])]
		end

		from:addToPile(self.name, toGain, true, self.name)
		room:obtainCard(effect.from, toGain, true, fk.ReasonPrey)
	end
}
  
--------------------------------------------------
--测试技能：流失体力
--------------------------------------------------

local v_lose = fk.CreateActiveSkill{
	name = "v_lose",
	anim_type = "offensive",
	can_use = function(self, player)
		return true
	end,
	target_num = 1,
	-- card_num = 1,
	card_filter = function(self, to_select, selected, selected_targets)
		return false
		-- if #selected == 1 then return false end
		-- return Fk:currentRoom():getCardArea(to_select) ~= Player.Equip
	end,
	target_filter = function(self, to_select, selected)
		-- local target = Fk:currentRoom():getPlayerById(to_select)
		return #selected == 0
	end,
	on_use = function(self, room, effect)
		local player = room:getPlayerById(effect.from)
		local target = room:getPlayerById(effect.tos[1])
		room:loseHp(target, 1, self.name)
	end,
}

--------------------------------------------------
--筹措
--------------------------------------------------

local v_choucuo = fk.CreateTriggerSkill{
  name = "v_choucuo",
  anim_type = "drawcard",
  events = {fk.DamageInflicted, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      return target == player and player:hasSkill(self) and data.from and 
      data.from ~= player
    elseif event == fk.EventPhaseStart then
      return target == player and player:hasSkill(self) and player.phase == Player.Start
    end
  end,
  on_cost = function(self, event, target, player, data)
		--if self:isEffectable(player) then
    local room = player.room
    if event == fk.DamageInflicted then
      local prompt = "#v_choucuo_choice:"..data.from.id
      self.cost_data = "v_choucuo_losehp"
      local card_ids = room:askForCard(player, 1, 1, true, self.name, true,"." ,prompt)
      if #card_ids > 0 then
        self.cost_data = card_ids[1]
      end
      return true
    elseif event == fk.EventPhaseStart then
      return true
    end
	end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted then
      -- local from = room:getPlayerById(data.from)
      local from = data.from
      if self.cost_data == "v_choucuo_losehp" then
        room:loseHp(player, 1, self.name)
      else 
        room:obtainCard(from, self.cost_data, true, fk.ReasonGive)
      end
    elseif event == fk.EventPhaseStart then
      local alive_players = #room.alive_players
      local d_c = math.ceil(alive_players / 2.0)
      room:drawCards(player, d_c, self.name)
    end
  end,
}

--------------------------------------------------
--横行
--------------------------------------------------

local v_hengxing_maxcards = fk.CreateMaxCardsSkill{
  name = "#v_hengxing_maxcards",
  correct_func = function(self, player)
    if not player:hasSkill(self) then return false end
    local num = #table.filter(Fk:currentRoom().alive_players, function(p)
      return p.chained
    end)
    return num
  end,
}

local v_hengxing = fk.CreateTriggerSkill{
  name = "v_hengxing",
  events = {fk.BeforeChainStateChange, fk.EventAcquireSkill, fk.EventLoseSkill},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == self and target == player
    end
    if not player:hasSkill(self) then return false end
    if event == fk.BeforeChainStateChange then
      return target == player and player.chained
    -- elseif target == player and player.phase == Player.Finish then 
    --   return table.find(player.room.alive_players, function(p) 
    --     return p ~= player and not p.chained
    --   end)
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill then
      player:setChainState(true)
    elseif event == fk.EventLoseSkill then
      player:setChainState(false)
    elseif event == fk.BeforeChainStateChange then
      return true
    -- else
    --   local availableTargets = table.map(table.filter(player.room.alive_players, function(p) 
    --     return p ~= player and not p.chained
    --   end), function(p)
    --     return p.id
    --   end)
    --   if #availableTargets == 0 then return false end
    --   local room = player.room
    --   local targets = room:askForChoosePlayers(player, availableTargets, 1, 1, "#jieying-target", self.name, false)
    --   room:getPlayerById(targets[1]):setChainState(true)
    end
  end,
}
v_hengxing:addRelatedSkill(v_hengxing_maxcards)

local pangxienayou_cishanjia = General(extension, "pangxienayou_cishanjia", "bisonpro", 3, 3, General.Bigender)
pangxienayou_cishanjia:addSkill(v_choucuo)
pangxienayou_cishanjia:addSkill(v_hengxing)

-- 加载本包的翻译包(load translations of this package)，这一步在本文档的最后进行。
-- dofile "packages/vupslash/i18n/init.lua"
dofile "packages/VslTest/i18n/init.lua"
  
-- return { extension } 

return extension